<?php
/**
 * 补报权限
 * @author jianhui
 *
 */
class AgainLimit extends MY_Controller{
    function AgainLimit(){
        parent::MY_Controller();
        $this->navbarFocus = 'SingleBudgetAgain';
    }
    /**
     * 普通补报权限列表
     */
    function normalList(){
        if(!$this->auth->isSingleNormalAgainLimitEdit()){
            showError('您没有补报申报权限');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AgainLimit', 'normalList'),'title'=>'普通补报列表');
        $this->limitList(1);
    }
    /**
     * 特殊补报权限列表
     */
    function specialList(){
        if (!$this->auth->isSingleSpacialAgainLimitEdit()){
            showError('您没有特殊补报申报权限');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AgainLimit', 'specialList'),'title'=>'特殊补报列表');
        $this->limitList(2);
    }
    /**
     * 区域补报列表
     */
    function areaList(){
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AgainLimit', 'specialList'),'title'=>'区域补报列表');
        $this->limitList(1,4);
    }
    /**
     * 不报权限列表
     * @param int $level 补报类型（1:普通补报,2:特殊补报）
     * @param number $userRole 开通补报角色
     */
    function limitList($level, $userRole = 0){
        if ($this->user['userRole'] == 1){
            showError('您无权限进去此页面');
        }
        //载入模型
        $this->load->model('supplementRoleModel');
        $this->load->model('sellpointModel');
        //查询条件
        $where = array();
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, '', $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $where['storeId'] = array('in', $placeStoreIds);
        //权限内专营店列表
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表,地区端只能查看自己添加的列表
        if ($this->user['userRole'] == $this->userLib->roleArea){
            $viewStoreIds = $this->userLib->getViewStoreIds();
            if ($viewStoreIds == '*'){
                $where['userId'] = array('in', $placeStoreIds);
            }else{
                $where['userId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
            }
        }
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] && $where['timeUnit'] = $this->viewData['timeUnit'];
        //年份条件
        $where['year'] = $year;
        $where['level'] = $level;
        $userRole && $where['userRole'] = $userRole;
        $count = $this->supplementRoleModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['page']);
        $config['base_url'] = printUrl($getParam['c'], $getParam['m'], $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        
        $this->viewData['supplementList'] = $this->supplementRoleModel->getNewList($where, $limit, $page, '', 'id desc');
        $this->load->view('AgainLimit/limitList', $this->viewData);
    }
    /**
     * 补报权限添加
     */
    function limitAdd(){
        if ($_POST){
            $where = array(
                'year'      => (int)$this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'level'     => (int)$this->input->post('level'),
            );
            $data = array(
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'userRole'  => $this->user['userRole'],
                'userId'    => $this->user['userId'],
                'isDel'     => 0,
                'createTime'=> time()
            );
            $where['year'] || showError('请选择年份');
            $where['timeUnit'] || showError('请选择双月');
            $where['level'] || showError('请选择补报类型');
            //补报时间
            $data['startTime'] || showError('请选择补报开始时间');
            $data['startTime'] = strtotime($data['startTime']);
            $data['endTime'] || showError('请选择补报结束时间');
            $data['endTime'] = strtotime($data['endTime']);
            $data['startTime'] < $data['endTime'] || showError('开始时间必须小于结束时间');
            if ($this->user['userRole'] == $this->userLib->roleArea){
                $this->load->model('timeUnitModel');
                $endime = $this->timeUnitModel->getTimeUnitEndTime($where['timeUnit'], $where['year']) + 86400*20;
                if($data['startTime'] > $endime ){
                    showError('您开通补报开始时间为双月结束后第20日24：00内');
                }
                if($data['endTime'] > $endime ){
                    showError('您开通补报结束时间为双月结束后第20日24：00内');
                }
            }
            $selectedStoreList = (array)$this->input->post('selectedStoreList');
            $selectedStoreList || showError('请选择要开通补报的专营店');
            $this->load->model('supplementRoleModel');
            foreach ($selectedStoreList as $v){
                $where['storeId'] = (int)$v;
                if ($where['storeId']){
                    $this->supplementRoleModel->upset($where, $data);
                }
            }
            if ($where['level'] == 1){
                showSuccess('添加成功', printUrl('AgainLimit', 'normalList'));
            }else if ($where['level'] == 2){
                showSuccess('添加成功', printUrl('AgainLimit', 'specialList'));
            }
        }else{
            $level = $this->input->get('level');
            $level || $level = 1;
            $this->viewData['level'] = $level;
            if ($level == 1){
                if(!$this->auth->isSingleNormalAgainLimitEdit()){
                    showError('您没有补报申报权限');
                }
                $this->viewData['breadcrumb'][] = array('url'=>printUrl('AgainLimit', 'limitAdd'),'title'=>'新增补报专营店');
            }
            if ($level == 2){
                if (!$this->auth->isSingleSpacialAgainLimitEdit()){
                    showError('您没有特殊补报申报权限');
                }
                $this->viewData['breadcrumb'][] = array('url'=>printUrl('AgainLimit', 'limitAdd'),'title'=>'新增特殊补报专营店');
            }
            //年份
            $year = $this->input->get('year');
            $year || $year = $this->config->item('nowYear');
            $this->viewData['year'] = $year;
            
            $this->load->view('AgainLimit/limitAdd', $this->viewData);
        }
    }
    /**
     * 权限限制
     */
    function limitEdit(){
        $this->load->model('supplementRoleModel');
        $this->load->model('sellpointModel');
        if ($_POST){
            $where = array(
                'id'        => (int)$this->input->post('id'),
                'year'      => (int)$this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'level'     => (int)$this->input->post('level'),
            );
            $data = array(
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'userRole'  => $this->user['userRole'],
                'userId'    => $this->user['userId'],
                'isDel'     => 0
            );
            $where['id'] || showError('请选择要修改的补报数据');
            //补报时间
            $data['startTime'] || showError('请选择补报开始时间');
            $data['startTime'] = strtotime($data['startTime']);
            $data['endTime'] || showError('请选择补报结束时间');
            $data['endTime'] = strtotime($data['endTime']);
            $data['startTime'] < $data['endTime'] || showError('开始时间必须小于结束时间');
            if ($this->user['userRole'] == $this->userLib->roleArea){
                $this->load->model('timeUnitModel');
                $endime = $this->timeUnitModel->getTimeUnitEndTime($where['timeUnit'], $where['year']) + 86400*20;
                if($data['startTime'] > $endime ){
                    showError('您开通补报开始时间为双月结束后第20日24：00内');
                }
                if($data['endTime'] > $endime ){
                    showError('您开通补报结束时间为双月结束后第20日24：00内');
                }
            }
            if ($this->supplementRoleModel->upset($where, $data)){
                showSuccess('修改补报成功');
            }else{
                showError('修改补报失败');
            }
        }else{
            $id = $this->input->get('id');
            $id || showError('请选择要修改的补报权限');
            $supplementData = $this->supplementRoleModel->getNewData($id);
            $supplementData || showError('补报权限不存在');
            $this->viewData['supplementData'] = $supplementData;
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($supplementData['storeId']);
            $this->load->view('AgainLimit/limitEdit', $this->viewData);
        }
    }
    /**
     * 补报权限关闭
     */
    function limitClose(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要关闭补报权限');
        $isDel = (int)$this->input->post('isDel');
        $this->load->model('supplementRoleModel');
        if ($this->supplementRoleModel->edit($id, array('isDel'=>$isDel))){
            showSuccess(($isDel==1?'关闭':'开通').'补报权限成功');
        }else{
            showError(($isDel==1?'关闭':'开通').'补报权限失败');
        }
    }
    /**
     * 审核通道列表
     */
    function auditList(){
        if(!$this->auth->isAuditLimitEdit()){
            showError('您没有审核通道管理权限');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AgainLimit', 'auditList'),'title'=>'审核权限列表');
        //载入模型
        $this->load->model('supplementAuditModel');
        //查询条件
        $where = array();
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] && $where['timeUnit'] = $this->viewData['timeUnit'];
        //年份条件
        $where['year'] = $year;
        $this->viewData['supplementList'] = $this->supplementAuditModel->getNewList($where, NULL, NULL, '', 'id desc');
        $this->load->view('AgainLimit/auditList', $this->viewData);
    }
    /**
     * 开通或修改审核通道
     */
    function auditAdd(){
        if(!$this->auth->isAuditLimitEdit()){
            showError('您没有审核通道管理权限');
        }
        //载入模型
        $this->load->model('supplementAuditModel');
        if ($_POST){
            $where = array(
                'year'      => (int)$this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
            );
            $data = array(
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'userRole'  => $this->user['userRole'],
                'userId'    => $this->user['userId'],
                'isDel'     => 0,
                'createTime' => time(),
            );
            $where['year'] || showError('请选择年份');
            $where['timeUnit'] || showError('请选择双月');
            $data['startTime'] || showError('请选择补报开始时间');
            $data['startTime'] = strtotime($data['startTime']);
            $data['endTime'] || showError('请选择补报结束时间');
            $data['endTime'] = strtotime($data['endTime']);
            if ($this->supplementAuditModel->upset($where, $data)){
                showSuccess('保存审核通道成功');
            }else{
                showError('保存审核通道失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $auditData = $this->supplementAuditModel->getNewData($id);
                $auditData || showError('您要修改的审核通道数据不存在');
                $this->viewData['auditData'] = $auditData;
            }
            $this->load->view('AgainLimit/auditAdd', $this->viewData);
        }
    }
    /**
     * 审核通道开通和关闭
     */
    function auditClose() {
        $id = (int)$this->input->post('id');
        $id || showError('请选择要关闭审核权限');
        $isDel = (int)$this->input->post('isDel');
        $this->load->model('supplementAuditModel');
        if ($this->supplementAuditModel->edit($id, array('isDel'=>$isDel))){
            showSuccess(($isDel==1?'关闭':'开通').'审核权限成功');
        }else{
            showError(($isDel==1?'关闭':'开通').'审核权限失败');
        }
    }
}